﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Tomato.Allure.Samples
{
    public partial class OpenAllForm : Form
    {
		private IVisualStudio m_visualStudio;
        private List<SolutionFileEntry> m_solutionFiles = null;

        private int m_filteredFileCount = 0;
        private string m_lastFilterString = "";
        private string m_currentFilterString = "";

        private bool m_bRebuildingFileList = false;

        public OpenAllForm( IVisualStudio visualStudio )
        {
            InitializeComponent();

			m_visualStudio = visualStudio;

			Location = new Point(
				m_visualStudio.MainWindow.Left + m_visualStudio.MainWindow.Width / 2 - Width / 2,
				m_visualStudio.MainWindow.Top + m_visualStudio.MainWindow.Height / 2 - Height / 2 );

            m_solutionFiles = new List<SolutionFileEntry>();
        }

        private void OpenSolutionFile_KeyDown( object sender, KeyEventArgs e )
        {
            switch( e.KeyCode )
            {
                case Keys.Escape:
                    {
                        DialogResult = DialogResult.Cancel;
                        e.Handled = true;
                    }
                    break;

                case Keys.Up:
                    {
                        if( comboBox1.Focused && listView1.Items.Count > 0 )
                        {
                            listView1.Items[ listView1.Items.Count - 1 ].Selected = true;
                            listView1.Items[ listView1.Items.Count - 1 ].Focused = true;
                            listView1.Focus();
                            e.Handled = true;
                        }
                    }
                    break;

                case Keys.Down:
                    {
                        if( comboBox1.Focused && listView1.Items.Count > 0 )
                        {
                            listView1.Items[ 0 ].Selected = true;
                            listView1.Items[ 0 ].Focused = true;
                            listView1.Focus();
                            e.Handled = true;
                        }
                    }
                    break;
            }

            e.Handled = false;
        }

        private void OpenSolutionFiles_VisibleChanged( object sender, EventArgs e )
        {
            if( Visible )
            {
                comboBox1.Text = m_currentFilterString;
                m_currentFilterString = m_lastFilterString;

                FilterFileList();

                if( !backgroundWorker1.CancellationPending && !backgroundWorker1.IsBusy )
                {
                    backgroundWorker1.RunWorkerAsync();
                }

				comboBox1.Focus();
            }
            else
            {
                backgroundWorker1.CancelAsync();
            }
        }

        private void backgroundWorker1_DoWork( object sender, DoWorkEventArgs e )
        {
            string lastFilterString = "";

            while( !backgroundWorker1.CancellationPending )
            {
                try
                {
                    if( m_currentFilterString != lastFilterString )
                    {
                        BeginInvoke( new UpdateViewDelegate( FilterFileList ) );

                        lastFilterString = m_currentFilterString;
                    }
                }
                catch( Exception ) { }

                System.Threading.Thread.Sleep( 500 );
            }

            e.Cancel = true;
        }

        private void comboBox1_TextChanged( object sender, EventArgs e )
        {
            m_currentFilterString = comboBox1.Text;
        }

        private void listView1_MouseDoubleClick( object sender, MouseEventArgs e )
        {
            AcceptButton.PerformClick();
        }

        private void OpenSolutionFilesWindow_KeyPress( object sender, KeyPressEventArgs e )
        {
            if( listView1.Focused
                && ( char.IsLetterOrDigit( e.KeyChar ) || ( char.IsPunctuation( e.KeyChar ) ) ) )
            {
                comboBox1.Focus();
                comboBox1.Text = e.KeyChar.ToString();
                comboBox1.Select( 1, 0 );
                e.Handled = true;
            }

            e.Handled = false;
        }
        
        // Button "Rebuild"
        private void button1_Click( object sender, EventArgs e )
        {
#if false
            button1.Enabled = false;
            m_bRebuildingFileList = true;

            RebuildSolutionFileList();

            m_bRebuildingFileList = false;
            button1.Enabled = true;
#endif
        }

        // Button "Open"
        private void button3_Click( object sender, EventArgs e )
        {
            OpenSelectedFiles();

            DialogResult = DialogResult.OK;
        }

        private void listView1_ItemSelectionChanged( object sender, ListViewItemSelectionChangedEventArgs e )
        {
            if( e.IsSelected )
            {
                button3.Enabled = true;
            }
        }

        private void comboBox1_KeyDown( object sender, KeyEventArgs e )
        {
            switch( e.KeyCode )
            {
                case Keys.Up:
                case Keys.Down:
                    {
                        e.Handled = true;
                    }
                    break;
            }
        }

		private void OpenSolutionFileForm_Load( object sender, EventArgs e )
		{
			comboBox1.Focus();
		}
    }
}